import torch
import torch.nn.functional as F
#torch.nn.functional的应用
input = torch.tensor([[1,2,0,3,1],
                      [0,1,2,3,1],
                      [1,2,1,0,0],
                      [5,2,3,1,1],
                      [2,1,0,1,1]])

kernel = torch.tensor([[1,2,1],
                       [0,1,0],
                       [2,1,0]])

#torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)
print(input.shape)#输出[5,5]
print(kernel.shape)#输出[3,3]

#convolution 2D的使用
#input – input tensor of shape (minibatch,in_channels,iH,iW)
#现在input和权重不满足需要的张量的形式，故进行以下的形式转换
input = torch.reshape(input,[1,1,5,5])
kernel = torch.reshape(kernel,[1,1,3,3])
print(input.shape)#输出[1,1,5,5]
print(kernel.shape)#输出[1,1,3,3]
#不同stride参数的影响
#stride为1的时候代表kernel对input进行卷积计算时向右和向下都是移动1
#stride=2表示kernel向右向下移动2
output = F.conv2d(input,kernel,stride=1)
print(output)

output2 = F.conv2d(input,kernel,stride=2)
print(output2)
#padding
output3 = F.conv2d(input,kernel,stride=1,padding=1)
print(output3)